package com.edgclub.edg.utils.util;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;

import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.squareup.otto.ThreadEnforcer;

public class MediaPlayerUtil implements SensorEventListener{

	private boolean isPlaying = false; // 播放状态
	public MediaPlayer mediaPlayer; // 媒体播放器
	private Context context;
	
	private AudioManager audioManager;
	private SensorManager mSensorManager;
	private Sensor mSensor;
	

	public MediaPlayerUtil(Context context) {
		super();
		this.context = GuZhiApplication.getInstance();
		audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		mSensorManager = (SensorManager) context
				.getSystemService(Context.SENSOR_SERVICE);
		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
		
	}

	public void playVoice(final String path) {
		if (!isPlaying) {
			mediaPlayer = new MediaPlayer();
			mSensorManager.registerListener(this, mSensor,
					SensorManager.SENSOR_DELAY_NORMAL);
			
			if(audioManager.isWiredHeadsetOn()){
				audioManager.setSpeakerphoneOn(false);
				 audioManager.setMode(AudioManager.ROUTE_HEADSET);
			}else{
				if (!OptionPrefUtil.getInstance(context).getModel()) {
					// 扬声器模式
					audioManager.setMode(AudioManager.MODE_NORMAL);
					audioManager.setSpeakerphoneOn(true);
//					mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
				} else {
					audioManager.setSpeakerphoneOn(false);// 关闭扬声器
					 audioManager.setMode(AudioManager.MODE_IN_CALL);
					audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
					
//					mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
				}
			}
			
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
			try {
				mediaPlayer.setDataSource(path);
				mediaPlayer.prepare();
				mediaPlayer.start();
				isPlaying = true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		/*	new Thread(new Runnable() {

				@Override
				public void run() {
					try {
//						mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
						mediaPlayer.setDataSource(path);
						mediaPlayer.prepare();
						mediaPlayer.start();
						isPlaying = true;
						// 设置播放结束时监听
//						mediaPlayer
//								.setOnCompletionListener(new OnCompletionListener() {
//
//									@Override
//									public void onCompletion(MediaPlayer mp) {
////										if (playState) {
////											playState = false;
////										}
//										stopPlayVoice();
//									}
//								});
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();*/
		} else {
			if (mediaPlayer!=null && mediaPlayer.isPlaying()) {
				stopPlayVoice();
			} else {
				isPlaying = false;
			}
		}
	}
	
	// 停止
	public void stopPlayVoice() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.reset();
			mediaPlayer.release();
			isPlaying = false;
			mediaPlayer = null;
		}
		//播放完设置回扬声器
		audioManager.setSpeakerphoneOn(true);
		audioManager.setMode(AudioManager.MODE_NORMAL);
		
		mSensorManager.unregisterListener(this);
		EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
				ThreadEnforcer.MAIN).post(BroadCastEvent.AudioManagerMODE_NORMAL);
		
	}

	public boolean isPlaying() {
		return isPlaying;
	}

	public void setPlaying(boolean isPlaying) {
		this.isPlaying = isPlaying;
	}

	public MediaPlayer getMediaPlayer() {
		return mediaPlayer;
	}

	public void setMediaPlayer(MediaPlayer mediaPlayer) {
		this.mediaPlayer = mediaPlayer;
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		float range = event.values[0];
		if (!OptionPrefUtil.getInstance(context).getModel() && !audioManager.isWiredHeadsetOn()) {
			if (range == mSensor.getMaximumRange()) {

				audioManager.setMode(AudioManager.MODE_NORMAL);
				audioManager.setSpeakerphoneOn(true);
				
				EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
						ThreadEnforcer.MAIN).post(BroadCastEvent.AudioManagerMODE_NORMAL);
			} else {
				audioManager.setSpeakerphoneOn(false);// 关闭扬声器
				// 把声音设定成Earpiece（听筒）出来，设定为正在通话中
				 audioManager.setMode(AudioManager.MODE_IN_CALL);
				audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
				
				EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
						ThreadEnforcer.MAIN).post(BroadCastEvent.AudioManagerMODE_IN_COMMUNICATION);
			}
		}
		
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	public AudioManager getAudioManager() {
		return audioManager;
	}

	public void setAudioManager(AudioManager audioManager) {
		this.audioManager = audioManager;
	}
	
	


}
